'''
fetchone()
fetchmany()
fetchall()的使用
cursorclass 设置游标返回类型
利用数据库的数据给类传参，解包语法
'''
import pymysql

class Student(object):

    def __init__(self,stuid,stusex,stuname):
        self.stuid = stuid
        self.stusex = stusex
        self.stuname = stuname

    def __str__(self):
        return f'{self.stuid}\t{self.stusex}\t{self.stuname}'

def main():
    # 1.创建数据库连接
    conn = pymysql.connect(host='192.168.221.137', port=3306,user='root',password='123456',
                           db='school',charset='utf8',
                           cursorclass=pymysql.cursors.DictCursor)
    try:
        # 2.返回游标对象，通过游标对象执行SQL语句，返回处理结果
        with conn.cursor() as cursor:
            # 3.执行SQL得到结果
            cursor.execute('select stuid,stusex,stuname from tb_student')
            for row in cursor.fetchall():
                # 例一
                # print(row['stuid'],end='\t')
                # print(row['stusex'],end='\t')
                # print(row['stuname'])
                # 例二 解包
                dept = Student(**row)
                print(dept)
    except pymysql.MySQLError as error:



        print(error)
    finally:
        # 5.关闭连接释放资源
        pass

if __name__ == '__main__':
    main()
